import 'package:base_moudle/index.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_swipe_action_cell/flutter_swipe_action_cell.dart';

import '../../../config/zs_routers.dart';
import '../../../models/data_model/HyWorkModel.dart';
import '../../../new_paltform/define/hy_header.dart';
import 'project_setting_logic.dart';

class HyProjectSettingPage
    extends HYGetXStatelessBasePage<HyProjectSettingLogic, Object> {
  static Future? push(List<ItemModel> arr) {
    return Get.toNamed(ZsRouter.zsProjectSettingRouter, arguments: arr);
  }

  @override
  String get naviTitle => '项目应用设置';

  @override
  List<Widget>? actions() {
    return [
      TextButton(
          onPressed: () {
            Get.back(result: controller.all);
          },
          child: ZsText(
            '完成',
            fontWeight: FontWeight.w500,
            fontSize: 16,
            color: zs_mainColor,
          ))
    ];
  }

  Widget _buildTopView(String title) {
    return ZsText(
      title,
      fontSize: 16,
      fontWeight: FontWeight.bold,
    ).intoContainer(
        alignment: Alignment.centerLeft,
        padding: const EdgeInsets.fromLTRB(16, 20, 16, 16));
  }

  @override
  Widget buildWidget(BuildContext? context, Object? object) {
    return SingleChildScrollView(
      child: Column(
        children: [
          _buildTopView('项目应用'),
          Obx(() =>
              _buildListView(isSelect: true, list: controller.selectList)),
          _buildTopView('更多'),
          Obx(() =>
              _buildListView(isSelect: false, list: controller.unselectList)),
        ],
      ),
    );
  }

  Widget _buildListView(
      {bool isSelect = true, required RxList<ItemModel> list}) {
    return HYContainer(
      margin: const EdgeInsets.all(16),
      backgroundColor: Colors.white,
      borderRadius: BorderRadius.circular(12),
      child: ReorderableListView.builder(
          itemBuilder: (BuildContext context, int index) {
            final ItemModel item = list[index];
            return SwipeActionCell(
                key: ValueKey(item),
                trailingActions: [
                  SwipeAction(
                      title: isSelect ? '移除' : '添加',
                      onTap: (CompletionHandler handler) async {
                        if (isSelect) {
                          if (list.length >= 2) {
                            await handler(true);
                            var md = list.removeAt(index);
                            md.isDisplay = false;
                            controller.unselectList.add(md);
                          } else {
                            EasyLoading.showToast('无法删除,最少保留一个');
                          }
                        } else {
                          await handler(true);
                          var md = list.removeAt(index);
                          md.isDisplay = true;
                          controller.selectList.add(md);
                        }
                      })
                ],
                backgroundColor: Colors.transparent,
                child: _buildCell(index, item, isSelect));
          },
          itemCount: list.length,
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          onReorderStart: (int c) {
            HapticFeedback.mediumImpact();
          },
          onReorderEnd: (int c) {
            HapticFeedback.mediumImpact();
          },
          onReorder: (int oldIndex, int newIndex) {
            if (oldIndex < newIndex) {
              newIndex -= 1;
            }
            final ItemModel child = list.removeAt(oldIndex);
            list.insert(newIndex, child);
          }),
    );
  }

  Widget _buildCell(int index, ItemModel model, bool isSelect) {
    return HYContainer(
      padding: const EdgeInsets.all(16),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              HYContainer(
                alignment: Alignment.center,
                backgroundColor: ColorHex('#FF4D64'),
                height: 20,
                width: 20,
                borderRadius: BorderRadius.circular(10),
                child: Icon(
                  isSelect ? Icons.remove : Icons.add,
                  color: Colors.white,
                  size: 14,
                ),
                onTap: () {
                  if (isSelect) {
                    if (controller.selectList.length >= 2) {
                      var md = controller.selectList.removeAt(index);
                      md.isDisplay = false;
                      controller.unselectList.add(md);
                    } else {
                      EasyLoading.showToast('无法删除,最少保留一个');
                    }
                  } else {
                    var md = controller.unselectList.removeAt(index);
                    md.isDisplay = true;
                    controller.selectList.add(md);
                  }
                },
              ),
              Image.asset(
                model.iconName,
                width: 40,
                height: 40,
              ).marginOnly(left: 12, right: 12),
              ZsText(model.title),
            ],
          ),
          Image.asset(
            'assets/images/project/work_store.png',
            width: 18,
            height: 14,
          ),
        ],
      ),
    );
  }
}
